package com.ss.android.b.a;

import android.content.Context;

import com.ss.android.b.bb;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.util.NetworkUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class BaseAdsData {
    public String webTitle;
    public int orientation;
    public String pkg;
    public String appName;
    public String downloadUrl;
    public String alertText;
    public String version;
    public String btnTxt;
    public String logExtra;
    public long J;
    public boolean K;
    private boolean hideIfExists;
    private boolean wifiOnly;
    public long id;
    public long adId;
    public int adType;
    public List trackUrlList;
    public List clickTrackUrlList;
    public String openUrl;
    public String webUrl;

    public BaseAdsData() {
        this.orientation = 0;
        this.K = true;
    }

    public static List a(String arg3) {
        List v0 = null;
        if (!android.support.design.a.isTextEmpty(arg3)) {
            try {
                ArrayList v1_1 = new ArrayList();
                if (bb.a().b(arg3)) {
                    v1_1.add(arg3);
                    return v1_1;
                }
                v0 = BaseAdsData.a(new JSONArray(arg3), null);
            } catch (Exception v1) {
            }
        }

        return v0;
    }

    public void initWithJO(JSONObject jo) {
        List v0_1;
        if (jo != null) {
            this.id = jo.isNull("id") ? jo.optLong("ad_id") : jo.optLong("id");
            this.adId = jo.optLong("ad_id");
            String type = jo.optString("type");
            if ("app".equals(type)) {
                this.adType = 1;
            } else if ("web".equals(type)) {
                this.adType = 2;
            } else if ("action".equals(type)) {
                this.adType = 3;
            } else if ("sdk".equals(type)) {
                this.adType = 3;
            }

            String[] v1 = new String[1];
            this.trackUrlList = BaseAdsData.a(jo, v1);
            if (jo == null) {
                v0_1 = null;
            } else {
                Object v0_2 = jo.opt("click_track_url_list");
                if (v0_2 == null) {
                    v0_2 = jo.opt("click_track_url");
                }

                v0_1 = BaseAdsData.a(v0_2, v1);
            }

            this.clickTrackUrlList = v0_1;
            this.openUrl = bb.a().a(jo.optString("open_url"));
            if (this.adType == 2) {
                this.webTitle = jo.optString("web_title");
                this.webUrl = jo.optString("web_url");
                this.orientation = jo.optInt("orientation", 0);
            } else if (this.adType == 1) {
                this.pkg = jo.optString("package");
                this.downloadUrl = jo.optString("download_url");
                this.alertText = jo.optString("alert_text");
                this.appName = jo.optString("app_name");
                this.hideIfExists = AbsApiThread.optBoolean(jo, "hide_if_exists", false);
                this.wifiOnly = AbsApiThread.optBoolean(jo, "wifi_only", false);
                this.version = jo.optString("version");
                this.btnTxt = jo.optString("button_text");
            }
            this.logExtra = jo.optString("log_extra");
        }
    }

    public static List a(Object in, String[] arg7) {
        ArrayList v0_1 = null;
        ArrayList result;
        int v2 = 0;
        String v0 = null;
        if (arg7 != null && arg7.length > 0) {
            arg7[0] = v0;
        }

        if (in != null) {
            try {
                if ((in instanceof String)) {
                    result = new ArrayList();
                    result.add(in);
                    if (arg7 != null && arg7.length > 0) {
                        arg7[0] = (String) in;
                    }

                    v0_1 = result;
                    return v0_1;
                }

                if (!(in instanceof JSONArray)) {
                    return v0_1;
                }

                int v3 = ((JSONArray) in).length();
                if (v3 == 0) {
                    return v0_1;
                }

                result = new ArrayList();
                while (v2 < v3) {
                    String v4 = ((JSONArray) in).getString(v2);
                    if (!android.support.design.a.isTextEmpty(v4)) {
                        ((List) result).add(v4);
                    }

                    ++v2;
                }

                if (((List) result).isEmpty()) {
                    return v0_1;
                }

                if (arg7 != null && arg7.length > 0) {
                    arg7[0] = in.toString();
                }
            } catch (Exception v1) {
                return v0_1;
            }
            v0_1 = result;
        }

        return v0_1;
    }

    public static List a(JSONObject jo, String[] arg2) {
        List v0;
        if (jo == null) {
            v0 = null;
        } else {
            Object trackUrls = jo.opt("track_url_list");
            if (trackUrls == null) {
                trackUrls = jo.opt("track_url");
            }
            v0 = BaseAdsData.a(trackUrls, arg2);
        }

        return v0;
    }

    public static void trackAdShow(Context context, String arg11, BaseAdsData arg12, int arg13) {
        JSONObject jo;
        if (arg12 != null && (arg12.isValid()) && context != null) {
            Context ctx = context.getApplicationContext();
            try {
                jo = new JSONObject();
                jo.put("log_extra", arg12.logExtra);
            } catch (Exception v0) {
                jo = null;
            }
            MobAdClickCombiner.onAdEvent(ctx, arg11, "show", arg12.id, 0, jo, arg13);
            bb.a().a(arg12.trackUrlList, ctx);
        }
    }

    public boolean isValid() {
        return this.id > 0
                && (this.adType == 1
                || this.adType == 2
                || this.adType == 3);
    }

    public boolean a(Context context) {
        if (this.adType == 1 && ((this.hideIfExists) || (this.wifiOnly))) {
            int v0 = this.J > 0 ? 1 : 0;
            v0 = v0 != 0 || !bb.a().a(context, this.pkg, this.openUrl) ? 0 : 1;
            if (!this.hideIfExists || v0 == 0) {
                if (!this.wifiOnly) {
                } else if (v0 != 0) {
                } else if (!NetworkUtils.isWifi(context)) {
                    return true;
                }

                return false;
            }
            return true;
        } else {
            return false;
        }
    }
}

